{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "表5-3提供管体常见缺陷类型及其推荐修复方法，涵盖腐蚀、制造缺陷、凹陷及焊缝缺陷等，并根据尺寸和安全状况建议机械夹具、套筒或换管等方式。\n"
     ]
    }
   ],
   "source": [
    "from openai import OpenAI\n",
    "\n",
    "client = OpenAI(\n",
    "    #api_key=os.getenv(\"DASHSCOPE_API_KEY\"),\n",
    "    api_key=\"ollama\",\n",
    "    base_url=\"http://192.168.20.43:11434/v1\"\n",
    ")\n",
    "\n",
    "def llm_summarize(context_str):\n",
    "    query = f\"\"\"\n",
    "    ## OBJECTIVE \n",
    "    - 提供一个简洁的句子摘要，概括CONTEXT的主要信息，\n",
    "    - 确保摘要准确捕捉到语义信息、关键概念和术语。\n",
    "\n",
    "    ## CONTEXT \n",
    "    {context_str}\\\n",
    "\n",
    "    ## 约束 \n",
    "    - 不要包括context中没有的内容. \\\n",
    "    - 摘要应控制在60字以内.\\\n",
    "    - 无需前言和总结.\\\n",
    "    - Reply in Chinese.\\\n",
    "    \"\"\"\n",
    "    prompt = f\"\"\"\n",
    "    \"\"\"\n",
    "\n",
    "    completion = client.chat.completions.create(\n",
    "        model=\"qwen2.5:14b\",\n",
    "        messages=[{'role': 'system', 'content': prompt},\n",
    "                  {'role': 'user', 'content': query}],\n",
    "        )\n",
    "    return completion.choices[0].message.content\n",
    "\n",
    "context_str = \"\"\"\n",
    "表5-3  管体常见缺陷类型推荐修复方法\n",
    "缺陷分类\t缺陷尺寸\t修复方法\n",
    "腐蚀\t外腐蚀\t泄漏\t机械夹具（临时修复）、B型套筒、环氧钢套筒或换管\n",
    "\t\t缺陷深度≥80%壁厚\tB型套筒、环氧钢套筒或换管\n",
    "\t\t超过允许尺寸的\t玻璃纤维复合材料补强、A型套筒、B型套筒、环氧钢套筒或换管\n",
    "\t\t未超过允许尺寸的\t粘弹体修复防腐层\n",
    "\t内腐蚀\t缺陷深度≥80%壁厚\tB型套筒或换管\n",
    "\t\t超过允许尺寸的\tB型套筒或换管\n",
    "\t\t当前或计划修复时间内未超过允许尺寸的\t暂不修复\n",
    "制造缺陷\t内外制造缺陷\t缺陷深度≥80%壁厚\tB型套筒、环氧钢套筒或换管\n",
    "\t\t超过允许尺寸的\t玻璃纤维复合材料补强、A型套筒、B型套筒、环氧钢套筒或换管\n",
    "\t\t未超过允许尺寸的\t暂不修复\n",
    "凹陷\t普通凹陷、腐蚀相关凹陷（移除压迫体后的尺寸）\t深度≥6%外径\tB型套筒（临时）或者换管\n",
    "\t\t2%外径≤深度＜6%外径\t进行磁粉探伤，无裂纹则采用A、B型或环氧套筒或者换管修复，有裂纹采用B型套筒或者换管修复\n",
    "\t\t深度＜2%外径\t巡线监控\n",
    "\t焊缝相关凹陷（移除压迫体后的尺寸）\t深度≥6%外径\tB型套筒（临时）或者换管\n",
    "\t\t2%外径≤深度＜6%外径\t进行表面磁粉探伤，焊缝进行射线或者超声，无裂纹则采用A、B型或环氧套筒或者换管修复，有裂纹采用B型套筒或者换管修复\n",
    "\t\t深度＜2%外径\t进行表面磁粉探伤，焊缝进行射线或者超声，无裂纹则不修复，有裂纹采用B型套筒或者换管修复\n",
    "焊缝缺陷\t开挖检测，采用射线和超声探伤得到焊接缺陷的长度、深度，进行缺陷强度评价\t不安全（有裂纹）\t换管\n",
    "\t\t安全（有裂纹）\t打磨（表面裂纹）、B型套筒和换管\n",
    "\t\t安全\t不修复\n",
    "\t开挖检测，采用射线和超声探伤得到焊接缺陷尺寸，未进行缺陷强度评价\t焊缝超过标准允许级别\t打磨（表面裂纹）、B型套筒和换管\n",
    "\t\t焊缝在标准允许级别内\t不修复\n",
    "\"\"\"\n",
    "\n",
    "summary_text = llm_summarize(context_str) \n",
    "#summary_dict = json.loads(summary_text)\n",
    "print(summary_text)\n",
    "#print(summary_dict)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
